package com.pk.platform.business.stock.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;

import com.pk.platform.business.common.service.IKindergartenService;
import com.pk.platform.business.stock.dao.IStockDao;
import com.pk.platform.business.stock.service.IStockService;
import com.pk.platform.business.stock.vo.FetchRecordVO;
import com.pk.platform.business.stock.vo.ImportRecordVO;
import com.pk.platform.business.stock.vo.StockObjVO;
import com.pk.platform.domain.common.Kindergarten;
import com.pk.platform.domain.stock.FetchRecord;
import com.pk.platform.domain.stock.ImportRecord;
import com.pk.platform.domain.stock.StockObj;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class StockServiceImpl implements IStockService {

	private IStockDao stockDao;
	
	private IKindergartenService kindergartenService;
	
	public ListPage queryFetchRecord(FetchRecordVO frvo, Pager pager) {
		return stockDao.queryFetchRecord(frvo, pager);
	}

	public ListPage queryImportRecord(ImportRecordVO irvo, Pager pager) {
		return stockDao.queryImportRecord(irvo, pager);
	}

	public ListPage queryStockObj(StockObjVO objvo, Pager pager) {
		return stockDao.queryStockObj(objvo, pager);
	}
	
	public List<Map<String, Object>> queryStockObjList(String kgId)
	{
		return stockDao.queryStockObjList(kgId);
	}
	
	public void addStockObj(StockObj so, String whickKinder)
	{
		if("this".equals(whickKinder))
		{
			stockDao.add(so);
		}
		else if("all".equals(whickKinder))
		{
			List<Kindergarten> kinderList = kindergartenService.queryAllKgList();
			for(Kindergarten kinder : kinderList)
			{
				StockObj stockObj = new StockObj();
				BeanUtils.copyProperties(so, stockObj);
				stockObj.setKindergarten(kinder);
				stockDao.add(stockObj);
			}
		}
		stockDao.addOperateLog("新增货存物品", Constant.SUCCESSFUL, so.getKindergarten());
	}

	public void updateStockObj(StockObj so)
	{
		StockObj obj = this.queryStockObjById(so.getId());
		obj.setObjName(so.getObjName());
		obj.setPrice(so.getPrice());
		obj.setStockCount(so.getStockCount());
		obj.setUnit(so.getUnit());
		obj.setChargeIndex(so.getChargeIndex());
		stockDao.addOperateLog("修改货存物品", Constant.SUCCESSFUL, obj.getKindergarten());
	}
	
	public void delStockObj(String[] ids)
	{
		Kindergarten kinder = null;
		for(String id : ids)
		{
			StockObj obj = this.queryStockObjById(id);
			if(kinder == null)
			{
				kinder = obj.getKindergarten();
			}
			obj.setIsDel(Constant.Y_INT);
		}
		stockDao.addOperateLog("删除货存物品", Constant.SUCCESSFUL, kinder);
	}
	
	public StockObj queryStockObjById(String id)
	{
		return stockDao.get(StockObj.class, id);
	}
	
	public String addFetchRecord(FetchRecord fr)
	{
		String result = "取货成功。";
		String objId = fr.getStockObj().getId();
		StockObj so = this.queryStockObjById(objId);
		double count = so.getStockCount();
		if(fr.getFetchCount() > count)
		{
			result = "已超过库存数量，库存还剩 " + count + " " + so.getUnit() + "。";
			stockDao.addOperateLog("新增取货记录", "失败，已超过库存数量", fr.getKindergarten());
		}
		else
		{
			stockDao.add(fr);
			so.setStockCount(count - fr.getFetchCount());
			stockDao.addOperateLog("新增取货记录", Constant.SUCCESSFUL, fr.getKindergarten());
		}
		return result;
	}

	public String delFetchRecord(String id)
	{
		FetchRecord fr = this.queryFetchRecordById(id);
		stockDao.addOperateLog("删除取货记录", Constant.SUCCESSFUL, fr.getKindergarten());
		StockObj so = fr.getStockObj();
		double count = so.getStockCount();
		so.setStockCount(count + fr.getFetchCount());
		stockDao.delete(FetchRecord.class, id);
		return null;
	}
	
	public void addImportRecord(ImportRecord fr)
	{
		stockDao.add(fr);
		stockDao.getHibernateSession().flush();
		StockObj so = this.queryStockObjById(fr.getStockObj().getId());
		double count = so.getStockCount();
		so.setStockCount(count + fr.getImportCount());
		stockDao.addOperateLog("新增进货记录", Constant.SUCCESSFUL, fr.getKindergarten());
	}
	
	public String delImportRecord(String id)
	{
		String result = "删除成功。";
		ImportRecord ir = this.queryImportRecordById(id);
		StockObj so = ir.getStockObj();
		double count = so.getStockCount();
		if(ir.getImportCount() > count)
		{
			result = "已超过库存数量，库存还剩 " + count + " " + so.getUnit() + "。";
			stockDao.addOperateLog("删除进货记录", "失败，已超过库存数量", ir.getKindergarten());
		}
		else
		{
			stockDao.addOperateLog("删除进货记录", Constant.SUCCESSFUL, ir.getKindergarten());
			so.setStockCount(count - ir.getImportCount());
			stockDao.delete(ImportRecord.class, id);
		}
		return result;
	}
	
	public FetchRecord queryFetchRecordById(String id)
	{
		return stockDao.get(FetchRecord.class, id);
	}

	public ImportRecord queryImportRecordById(String id)
	{
		return stockDao.get(ImportRecord.class, id);
	}

	public IStockDao getStockDao() {
		return stockDao;
	}

	public void setStockDao(IStockDao stockDao) {
		this.stockDao = stockDao;
	}

	public IKindergartenService getKindergartenService() {
		return kindergartenService;
	}

	public void setKindergartenService(IKindergartenService kindergartenService) {
		this.kindergartenService = kindergartenService;
	}

}
